/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **      It is a Atomic action to check session server status 
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.exception.BusinessException;
import com.hp.snap.gr.model.BizServiceStatus;
import com.hp.snap.gr.model.SessionServerStatus;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.GeneralActionType;
import com.hp.snap.gr.model.jaxb.ParameterType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * It is a Atomic action to check session server status. The action need to configure a "Enable_Status" parameter as
 * checking condition, the "Enable_Status" should be configured to "STOPPED","ACTIVE" value. Get the session server
 * status map from action plan output, in the previous action, the session server status map can be save in the action
 * plan output. If the session server status does not match the Enable_status value, it will result in a
 * BusinessException and interrupt the execution.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class CheckSessionServerStatusEnableActionHandler extends
        AbstractActionHandler {

    private static Logger logger = LoggerFactory.getLogger(CheckSessionServerStatusEnableActionHandler.class);


    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {

        GeneralActionType actionType = (GeneralActionType) action;
        List<ParameterType> parameterTypes = actionType.getParameter();
        Map<String, String> parameterMap = Utils.contructParamMap(parameterTypes);

        String status = parameterMap.get("Enable_Status");
        boolean stateFlag = true;
        try {
            SessionServerStatus.ProcessStatus.valueOf(status);
            stateFlag = true;
        } catch (IllegalArgumentException e) {
            try {
                SessionServerStatus.KernelStatus.valueOf(status);
                stateFlag = false;
            } catch (IllegalArgumentException e2) {
                logger.error("set the enable session status:{} is wrong, please you check the configuration file!!", status);
                throw new ActionException("set the enble status value wrong!!");
            }
        }

        Map<String, SessionServerStatus> ssStatusMap = (Map<String, SessionServerStatus>) actionPlanOutputs.get(Constants.STATUS_SESSION_SERVER);
        Map<String, Integer> ssTypeStartCounter = Utils.getSSTypeCounter(ssStatusMap.keySet());
        boolean ssStatusIsOK = true;
        if (ssStatusMap.size() == 0) ssStatusIsOK = false;
        for (Map.Entry<String, SessionServerStatus> entry : ssStatusMap.entrySet()) {
            SessionServerStatus masterStatus = entry.getValue();
            if (logger.isDebugEnabled()) {
                logger.debug(" SS name: {}, State:{}, Status:{}, BizServiceStatus:{}", new Object[]{masterStatus.getName(), masterStatus.getProcessStatus(), masterStatus.getKernelStatus(), masterStatus.getBizServiceStatus()});
            }
            if (stateFlag) {
                if (SessionServerStatus.ProcessStatus.valueOf(status) != masterStatus.getProcessStatus())
                    ssStatusIsOK &= false;
            } else {
                if (SessionServerStatus.KernelStatus.ACTIVE == SessionServerStatus.KernelStatus.valueOf(status)) {
//                    if (SessionServerStatus.KernelStatus.ACTIVE == masterStatus.getKernelStatus() && BizServiceStatus.Running == masterStatus.getBizServiceStatus()) {
                    if (SessionServerStatus.KernelStatus.ACTIVE == masterStatus.getKernelStatus()) {
                        String ssType = Utils.getSSType(masterStatus.getName());
                        ssTypeStartCounter.put(ssType, ssTypeStartCounter.get(ssType) + 1);
                    }
                } else {
                    if (SessionServerStatus.KernelStatus.valueOf(status) != masterStatus.getKernelStatus())
                        ssStatusIsOK &= false;
                }

            }

        }

        if (!stateFlag && SessionServerStatus.KernelStatus.ACTIVE == SessionServerStatus.KernelStatus.valueOf(status)) {

            for (String ssType : ssTypeStartCounter.keySet()) {
                if (ssTypeStartCounter.get(ssType) == 0)
                    ssStatusIsOK &= false;
            }
            Utils.printSSTypeCounter(ssTypeStartCounter);
        }
        if (!ssStatusIsOK) {
            throw new BusinessException("Some session server status are not " + status + "!!");
        }

    }

}
